/*------------------------------------------------------------------------------*\
**   Copyright 2008 Jonhnny Weslley                                             **
**                                                                              **
**   Licensed under the Apache License, Version 2.0 (the "License");            **
**   you may not use this file except in compliance with the License.           **
**   You may obtain a copy of the License at                                    **
**                                                                              **
**       http://www.apache.org/licenses/LICENSE-2.0                             **
**                                                                              **
**   Unless required by applicable law or agreed to in writing, software        **
**   distributed under the License is distributed on an "AS IS" BASIS,          **
**   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   **
**   See the License for the specific language governing permissions and        **
**   limitations under the License.                                             **
\*------------------------------------------------------------------------------*/

package shibernate.singleton

import shibernate._
import java.io.Serializable
import org.hibernate.Session

object GenericDAO {

  private def session = SessionManager.getSession

  def save(persist: AnyRef) {
    session saveOrUpdate persist
  }

  def delete(persist: AnyRef) {
    session delete persist
  }

  def findById[A](id: Any, entityClass: Class[A]): A = {
    val serialId = id.asInstanceOf[Serializable]
    session.get(entityClass, serialId).asInstanceOf[A]
  }

  def list[A](param: Parameter, entityClass: Class[A]) = {
    Search(session, entityClass, param)
  }

  def list[A](entityClass: Class[A]) = {
    Search(session, entityClass, null)
  }

  def transaction(doInTransaction: => Unit) = {
    val t = session.beginTransaction
    doInTransaction
    t.commit
  }

}
